﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QuanLyBanHang.DAL;
using QuanLyBanHang.OBJECT;
namespace QuanLyBanHang
{
    public partial class frmHangHoa : Form
    {
        private DataTable dt;
        DataTable dulieu;

        public frmHangHoa()
        {
            InitializeComponent();
            dulieu = null;
        }

        public frmHangHoa(DataTable dt)
        {
            InitializeComponent();
            dulieu = dt;
        }
        private void frmHangHoa_Load(object sender, EventArgs e)
        {
            if (dulieu == null)
            {
                LoadData();
                DgvClick();
            }
            else
            {
                dgvDSHH.Rows.Clear();
                for (int i = 0; i < dulieu.Rows.Count; i++)
                {
                    string ma = dulieu.Rows[i][0].ToString();
                    string ten = dulieu.Rows[i][1].ToString();
                    string gianhap = dulieu.Rows[i][2].ToString();
                    string giaban = dulieu.Rows[i][3].ToString();
                    DateTime ngaysx = (DateTime)dulieu.Rows[i][4];
                    DateTime hansd = (DateTime)dulieu.Rows[i][5];
                    string soluong = dulieu.Rows[i][6].ToString();
                    string anh = dulieu.Rows[i][7].ToString();
                    string gc = dulieu.Rows[i][8].ToString();
                    string lh = dulieu.Rows[i][9].ToString();
                    string ncc = dulieu.Rows[i][10].ToString();
                    string kho = dulieu.Rows[i][11].ToString();
                    string dvt = dulieu.Rows[i][12].ToString();
                    dgvDSHH.Rows.Add(new Bitmap(Image.FromFile("images\\hanghoa\\" + anh)), ma, ten, giaban, ngaysx, hansd, soluong, gianhap,
                        gc, lh, ncc, kho, dvt);
                    DgvClick();
                }
            }
        }
        private void DgvClick()
        {
            PicHH.ImageLocation = @"images\\hanghoa\\" + dgvDSHH.CurrentRow.Cells[1].Value.ToString() + ".jpg";
        }
        public void LoadData()
        {
            DataTable dt = DalHangHoa.LoadProducts();
            dgvDSHH.Rows.Clear();
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                string ma = dt.Rows[i][0].ToString();
                string ten = dt.Rows[i][1].ToString();
                string gianhap = dt.Rows[i][2].ToString();
                string giaban = dt.Rows[i][3].ToString();
                DateTime ngaysx = (DateTime)dt.Rows[i][4];
                DateTime hansd = (DateTime)dt.Rows[i][5];
                string soluong = dt.Rows[i][6].ToString();
                string anh = dt.Rows[i][7].ToString();
                string gc = dt.Rows[i][8].ToString();
                string lh = dt.Rows[i][9].ToString();
                string ncc = dt.Rows[i][10].ToString();
                string kho = dt.Rows[i][11].ToString();
                string dvt = dt.Rows[i][12].ToString();
                dgvDSHH.Rows.Add(new Bitmap(Image.FromFile("images\\hanghoa\\"+anh)),ma,ten,giaban,ngaysx,hansd,soluong,gianhap,
                    gc,lh,ncc,kho,dvt);
                DgvClick();
            }
        }
        private void dgvDSHH_CellClick(object sender, DataGridViewCellEventArgs e)
        {
            DgvClick();
        }

        private void thêmToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCapNhatHangHoa.Action = 1;
            frmCapNhatHangHoa cn = new frmCapNhatHangHoa(dgvDSHH, this);
            cn.ShowDialog();
        }

        private void sửaHàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            frmCapNhatHangHoa.Action = 2;
            frmCapNhatHangHoa cn = new frmCapNhatHangHoa(dgvDSHH, this);
            cn.ShowDialog();
        }

        private void xóaHàngHóaToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult dr = MessageBox.Show("Bạn có muốn xóa mặt hàng: " + dgvDSHH.CurrentRow.Cells[2].Value.ToString().ToUpper() + " không?", "Thông báo", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.Yes)
            {
                ObjHangHoa obj = new ObjHangHoa();
                obj.Ma = dgvDSHH.CurrentRow.Cells[1].Value.ToString();
                int thucthi = DalHangHoa.DeleteProducts(obj);
                if (thucthi > 0)
                {
                    MessageBox.Show("Mặt hàng: " + dgvDSHH.CurrentRow.Cells[2].Value.ToString().ToUpper() + " đã bị xóa khỏi CSDL", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    frmHangHoa_Load(sender, e);
                }
                else
                    MessageBox.Show("Mặt hàng: " + dgvDSHH.CurrentRow.Cells[2].Value.ToString().ToUpper() + " chưa được xóa khỏi CSDL", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
            }
            
        }

        protected override bool ProcessDialogKey(Keys keyData)
        {
            if (keyData == Keys.Escape)
            {
                this.Close();
                return base.ProcessDialogKey(keyData);
            }
            else
                return false;
        }
    }
}
